package com.sh.data.engine.domain.normalization.modeldesign.model.service.impl;

import com.sh.data.engine.common.enumDefinition.DSType;
import com.sh.data.engine.domain.normalization.modeldesign.model.model.domain.CreateTableDomain;
import com.sh.data.engine.domain.normalization.modeldesign.model.model.domain.ModelDomain;
import com.sh.data.engine.domain.normalization.modeldesign.model.model.param.SaveModelParam;
import com.sh.data.engine.domain.normalization.modeldesign.model.service.TableOperationStrategy;
import com.sh.data.engine.domain.shims.db.BaseDbManager;
import com.sh.data.engine.domain.shims.db.model.TableInfoDomain;

import java.util.List;

/**
 *
 */
public class TableOperation {
    private final TableOperationStrategy tableOperationStrategy;

    public TableOperation(DSType type) {
        if (type == DSType.Hive) {
            this.tableOperationStrategy = new HiveTableOperation();
        } else if (type == DSType.PostgreSQL || type == DSType.MatrixDB || type == DSType.KINGBASE8) {
            this.tableOperationStrategy = new PgTableOperation();
        } else if (type == DSType.TiDB) {
            this.tableOperationStrategy = new TiDBTableOperation();
        } else if (type == DSType.Sap) {
            this.tableOperationStrategy = new SapTableOperation();
        } else if (type == DSType.DM) {
            this.tableOperationStrategy = new DMTableOperation();
        } else {
            throw new UnsupportedOperationException("not supported db type");
        }
    }

    TableInfoDomain getTableInfoDomain(
        String dbName, String schema, String tableName, BaseDbManager dbManager) {
        return this.tableOperationStrategy.getTableInfoDomain(dbName, schema, tableName, dbManager);
    }

    List<ModelDomain.Field> getTableField(
        String dbName, String schema, String tableName, BaseDbManager dbManager) {
        return this.tableOperationStrategy.getTableField(dbName, schema, tableName, dbManager);
    }

    boolean isTableNameExist(
        String dbName, String schema, String tableName, BaseDbManager dbManager) {
        return this.tableOperationStrategy.isTableNameExist(dbName, schema, tableName, dbManager);
    }

    boolean createTable(CreateTableDomain createTableDomain) {
        return this.tableOperationStrategy.createTable(createTableDomain);
    }

    boolean editFormTable(
        String dbName,
        String schema,
        ModelDomain dbModel,
        SaveModelParam saveModelParam,
        BaseDbManager dbManager) {
        return this.tableOperationStrategy.editFormTable(
            dbName, schema, dbModel, saveModelParam, dbManager);
    }

    boolean dropTable(String dbName, String schemaName, String tableName, BaseDbManager dbManager) {
        return this.tableOperationStrategy.dropTable(dbName, schemaName, tableName, dbManager);
    }

    ModelServiceImpl.FieldsParseResult parseFields(
        ModelDomain dbModel, SaveModelParam saveModelParam) {
        return this.tableOperationStrategy.parseFields(dbModel, saveModelParam);
    }
}
